package asteroids.expression;

import asteroids.model.programs.*;

public abstract class BooleanComparator extends BooleanExpression {
	private Expression arg1;
	private Expression arg2;
	Expression outArg1;
	Expression outArg2;
	Compatibility comp;

	public enum Compatibility {
		NUMERIC, ENTITY, VARIABLE, NONE
	}

	public BooleanComparator(Expression expression1, Expression expression2) {
		if ((expression1 instanceof NumericExpression)
				&& (expression2 instanceof NumericExpression)) {
			this.comp = Compatibility.NUMERIC;
			this.arg1 = expression1;
			this.arg2 = expression2;
		} else if ((expression1 instanceof EntityExpression)
				&& (expression2 instanceof EntityExpression)) {
			this.comp = Compatibility.ENTITY;
			this.arg1 = expression1;
			this.arg2 = expression2;

		} else if (((expression1 instanceof Variable) || (expression2 instanceof Variable))
				&& !(expression1 instanceof BooleanExpression)
				&& !(expression2 instanceof BooleanExpression)) {
			this.comp = Compatibility.VARIABLE;
			this.arg1 = expression1;
			this.arg2 = expression2;
		} else {
			this.comp = Compatibility.NONE;

		}
	}

	public void updateArguments(Program program) {
		Expression tempArg1=this.arg1;
		Expression tempArg2=this.arg2;
			
			if(arg1 instanceof Variable && arg2 instanceof Variable){
				if((((Variable)this.arg1).getRef(program) instanceof NumericExpression)&&(((Variable)this.arg2).getRef(program) instanceof NumericExpression)){
					this.comp=Compatibility.NUMERIC;
					tempArg1=((Variable)this.arg1).getRef(program);
					tempArg2=((Variable)this.arg2).getRef(program);
					
				}else if((((Variable)this.arg1).getRef(program) instanceof EntityExpression)&&(((Variable)this.arg2).getRef(program) instanceof EntityExpression)){
					this.comp=Compatibility.ENTITY;
					tempArg1=((Variable)this.arg1).getRef(program);
					tempArg2=((Variable)this.arg2).getRef(program);
				}else{
					this.comp=Compatibility.NONE;
				}
			}else if (this.arg1 instanceof Variable) {
				
				if(((Variable)this.arg1).getRef(program) instanceof NumericExpression){
					if(this.arg2 instanceof NumericExpression){
						this.comp=Compatibility.NUMERIC;
						tempArg1=((Variable)this.arg1).getRef(program);
					}
					
				}else if(((Variable)this.arg1).getRef(program) instanceof EntityExpression){
					if(this.arg2 instanceof EntityExpression){
						this.comp=Compatibility.ENTITY;
						tempArg1=((Variable)this.arg1).getRef(program);
					}
					
				}else{
					this.comp=Compatibility.NONE;
				}

			} else {
				
				if(((Variable)this.arg2).getRef(program) instanceof NumericExpression){
					if(this.arg1 instanceof NumericExpression){
						this.comp=Compatibility.NUMERIC;
						tempArg2=((Variable)this.arg2).getRef(program);
					}
					
				}else if(((Variable)this.arg2).getRef(program) instanceof EntityExpression){
					if(this.arg1 instanceof EntityExpression){
						this.comp=Compatibility.ENTITY;
						tempArg2=((Variable)this.arg2).getRef(program);
					}
					
				}else{
					this.comp=Compatibility.NONE;
				}

			}
		
		this.outArg1=tempArg1;
		this.outArg2=tempArg2;
	}
}